// pages/idPhoneN/idPhoneN.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",//输入的手机号
    code:"",//输入的验证码
    phoneErr: false,//显示手机错误信息
    // secuCode: "",//获取得到的验证码
    codeTrue: false,//显示验证码
    codeImg: "",//验证码图片
    codeTrue: true//显示获取验证码按钮

  },
//验证手机合法性
  phoneBlur(e){
    let that = this;
    var regExp = new RegExp("^1[3-9]\\d{9}");//验证手机号是否合法
    console.log(e);
    that.setData({
      phone: e.detail.value
    })
    if (!regExp.test(that.data.phone)){
      that.setData({
        phoneErr: true
      })
      // 提示消失
      setTimeout(function(){
        that.setData({
          phoneErr: false
        })
      },1500)
    }
  },

  //获取验证码
  getCode(){
    let that = this;
    let num = Math.floor(Math.random()*5);
    that.setData({
      codeTrue: false
    },function(){
      that.setData({
        codeImg: '/assets/img/'+num+'.jpg'
      })
    })
    // // let codes = [1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j'];
    // let len = codes.length;
    // var num = "";
    // for(var i=0; i<4; i++){
    //   let idx = Math.floor(Math.random()*len);
    //   console.log(idx);
    //   num += codes[idx];
    // }
    // console.log(num)
    // that.setData({
    //   secuCode: num,
    //   getC: true
    // })
    // console.log(that.data.secuCode);
  },
  //验证验证码是否正确
  codeBlur(e){
    let that = this;
    that.setData({
      code: e.detail.value
    })
    
  },
  // 完成按钮
  onSubmit(){
    let that = this;
    if (that.data.code && that.data.phone){
      wx.showToast({
        title: '绑定成功',
        duration: 1500,
        mask: true
      })
      //跳转页面
      setTimeout(function () {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }, 1500)
    }else{
      wx.showToast({
        title: '绑定失败',
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }
  }
})